<template>
  <list-detail-layout >
    <template #detailMain>
      <strategy-detail-main :items="formItems" :loadingData="spinningLoading"></strategy-detail-main>
    </template>
    <template #detailRight>
      <strategy-detail-base-info :detailBaseInfoData="detailBaseInfoData" :loading-data="spinningLoading">
      </strategy-detail-base-info>
    </template>
  </list-detail-layout>
</template>
<script>
import ListDetailLayout from '@/views/components/ListDetailLayout'
import StrategyDetailMain from './StrategyDetailMain'
import StrategyDetailBaseInfo from './StrategyDetailBaseInfo'
export default {
  components: {
    StrategyDetailMain,
    StrategyDetailBaseInfo,
    ListDetailLayout
  },
  props: {
    id: {
      type: String,
      default: ''
    },
    isCopy: {
      type: Boolean,
      default: false
    },
    time: {
      type: Number,
      default: 784502
    },
    versionType: {
      type: String,
      default: ''
    },
    upgradeTriggerType: {
      type: String,
      default: ''
    },
  },
  computed: {
    detailBaseInfoData () {
      if (!this.editRecord) {
        return {}
      }
      const keys = ['name', 'createTime', 'createBy']
      const info = {}
      keys.forEach(item => {
        info[item] = this.editRecord[item]
      })
      return info
    }
  },
  watch: {
    time: {
      handler () {
        this.getStrateDetail()
      },
      immediate: true
    }
  },
  data () {
    return {
      spinningLoading: false,
      editRecord: null,
      formItems: []
    }
  },
  methods: {
    // 获取策略详情数据
    getStrateDetail () {
      this.formItems = []
      this.editRecord = null
      if (!this.id) {
        return
      }
      this.spinningLoading = true
      this.$store.dispatch('getStrategyDetailData', {id : this.id,versionType:this.versionType, upgradeTriggerType:this.upgradeTriggerType}).then(res => {
        const data = res.data
        if (!data) {
          return
        }
        const obj = {
          id: this.id,
          name: data.name,
          createTime: data.createTime,
          createBy: data.createBy
        }
        this.formItems = data.items ? data.items : []
        this.editRecord = obj
      }).finally(() => {
        this.spinningLoading = false
      })
    }
  }
}
</script>
